package com.example.lcode.binary;

public class Question162 {


    public int findPeakElement(int[] nums) {
        return findPeakElement(nums, 0, nums.length - 1);
    }

    private int findPeakElement(int[] nums, int left, int right) {
        if (left >= right) {
            return left;
        }
        int mid = (left + right) / 2;
        if (mid == 0 && nums[mid] > nums[mid + 1] || mid == nums.length - 1 && nums[mid] > nums[mid - 1]) {
            return mid;
        } else if (mid != 0 && mid != nums.length - 1
        && nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]) {
            return mid;
        } else if (mid != nums.length - 1 && nums[mid] < nums[mid + 1]) {
            return findPeakElement(nums, mid + 1, right);
        } else {
            return findPeakElement(nums, left, mid - 1);
        }
    }

    public static void main(String[] args) {

    }


}
